package com.evil.formula.antlr4;

import com.evil.formula.service.impl.MyFormulaVisitor;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;

/**
 * @author liyang
 * @date 2023-10-23 16:29
 */
public class DemoMain {
    public static void run(String expr) throws Exception {
        // 构造一个字符流
        CodePointCharStream codePointCharStream = CharStreams.fromString(expr);
        // 用 in 构造词法分析器 lexer，词法分析的作用是将字符聚集成单词或者符号
        FormulaLexer lexer = new FormulaLexer(codePointCharStream);
        // 用词法分析器 lexer 构造一个记号流 tokens
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        // 再使用 tokens 构造语法分析器 parser,至此已经完成词法分析和语法分析的准备工作
        FormulaParser parser = new FormulaParser(tokens);
        // 最终调用语法分析器的规则 parse（在demo.g4里定义的规则），完成对表达式的验证
        FormulaParser.FormulaContext context = parser.formula();

        Object accept = context.accept(new MyFormulaVisitor());
        System.out.println("");
    }

    public static void main(String[] args) throws Exception {
        String[] testStr = {
                "3+3-2*6"
        };
        for (String s : testStr) {
            System.out.println("Input: " + s);
            run(s);
        }
    }
}
